"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.postParser = void 0;
const finch_tools_1 = require("finch-tools");
const fs = require("fs");
const path = require("path");
const Busboy = require("busboy");
const uploadPath = './upload';
function postParser(req, res, next) {
    if (req.method !== "POST") {
        next();
        return;
    }
    req.postFields = {};
    req.postFiles = [];
    if (!fs.existsSync(uploadPath)) {
        fs.mkdirSync(uploadPath);
    }
    let busboy = Busboy({ headers: req.headers });
    busboy.on('file', function (fileName, fileStream, fileInfo) {
        let FN = (0, finch_tools_1.getRandomString)();
        let saveTo = path.join("./upload", FN);
        fileStream.pipe(fs.createWriteStream(saveTo));
        let fi = { fileName: FN, orgName: fileInfo };
        req.postFiles.push(fi);
    });
    busboy.on('field', function (fieldName, value, fieldInfo) {
        req.postFields[fieldName] = value;
    });
    busboy.on('finish', function () {
        next();
    });
    req.pipe(busboy);
}
exports.postParser = postParser;
